/*
 * wiznet.c
 *
 *  Created on: Sep 20, 2013
 *      Author: Ken Arok
 */

#include <string.h>
#include "io.h"
#include "wiznet.h"


#define WIZNET_COM_PORT		IOP_COM1
#define WIZNET_SIGNALING	xUART0Signaling

static uint8_t _RecvMessage[100];

static 	uint32_t _rx_timeout = 50;

void vWiznetInit(void)
{
	/* Open serial port for Wiznet. */
	vIO_Open(WIZNET_COM_PORT, 0);
	/* Load serial setting. */
	vIO_Ctrl(WIZNET_COM_PORT, IORC_LOAD_SETTING_FROM_ROM, NULL);
	/* Set to interrupt transfer for receiver. */
	vIO_Ctrl(WIZNET_COM_PORT, IORC_SET_TRANSFER_POLLED_TX, NULL);
	/* Set Receiver timeout infinite. */
	_rx_timeout = 0xFFFFFFFE;
	vIO_Ctrl(WIZNET_COM_PORT, IORC_SET_RX_TIMEOUT, (void *)&_rx_timeout);
}


bool vWiznetIsMessageReceived(void)
{
	if(WIZNET_SIGNALING == true) {
		WIZNET_SIGNALING = false;
		return true;
	}
	else return false;
}

void *vWiznetReceivedMessage(void)
{
	return (void *)_RecvMessage;
}

void vWiznetStartListen(void)
{
	/* Start Listen. */
	_rx_timeout = 20;
	memset((void *)_RecvMessage, 0, sizeof(_RecvMessage));
	vIO_Ctrl(WIZNET_COM_PORT, IORC_SET_RX_TIMEOUT, (void *)&_rx_timeout);
	vIO_Read(WIZNET_COM_PORT, (void *)_RecvMessage, 60, IORWF_BYTE_ORDER);
}
